package edu.jmu.service.sysmanage.impl;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;

import edu.jmu.bean.system.MenuNode;
import edu.jmu.service.base.DaoSupport;
import edu.jmu.service.sysmanage.MenuNodeService;

/**
 * 菜单节点业务类
 *  
 * @author Neal
 * 
 */
@Service
public class MenuNodeServiceBean extends DaoSupport<MenuNode> implements
		MenuNodeService {

	@SuppressWarnings("unchecked")
	@Override
	public List<MenuNode> getMenuNodes() {
		String jpql = "select o from MenuNode o order by o.parentNode.id,o.position";

		List<MenuNode> menuNodes = em.createQuery(jpql).getResultList();

		// 父节点
		List<MenuNode> parentNodes = new LinkedList<MenuNode>();
		int beginChildIndex = 0;
		for (int i = 0; i < menuNodes.size(); i++) {
			if (menuNodes.get(i).getParentNode() != null) {
				beginChildIndex = i;
				break;
			}
			parentNodes.add(menuNodes.get(i));
		}

		// 子节点添加
		for (; beginChildIndex < menuNodes.size(); beginChildIndex++) {
			for (int i = 0; i < parentNodes.size(); i++) {
				if (parentNodes.get(i).getId() == menuNodes
						.get(beginChildIndex).getParentNode().getId()) {
					parentNodes.get(i).addChildNode(
							menuNodes.get(beginChildIndex));
					break;
				}
			}
		}

		// for(MenuNode parentNode : parentNodes){
		// System.out.print(parentNode.getName() + "  :  ");
		// for(MenuNode childNode : parentNode.getChildNodes()){
		// System.out.print(childNode.getName() + "  ");
		// }
		// System.out.println();
		// }

		return parentNodes;
	}

	public void editMenuNodePosition(int nodeId, int originalPosition,
			int targetPosition) {
		String jpqlUpdateNode = null;
		String jpqlSetNode = null;
		MenuNode node = find(nodeId);
		Query query1 = null;
		if (originalPosition - targetPosition > 0) {
			// 上移 === 更新位置大于目标节点的节点位置
			if (null != node.getParentNode()
					&& !"".equals(node.getParentNode())) {
				jpqlUpdateNode = "update MenuNode as o set o.position = o.position + 1 where o.parentNode.id = ?1 and o.position >= ?2 and o.position <= ?3";
			} else {
				jpqlUpdateNode = "update MenuNode as o set o.position = o.position + 1 where o.parentNode is null and o.position >= ?2 and o.position <= ?3";
			}
		} else {
			// 下移   === 更新位置小于目标节点的节点位置
			if (null != node.getParentNode()
					&& !"".equals(node.getParentNode())) {
				jpqlUpdateNode = "update MenuNode as o set o.position = o.position - 1 where o.parentNode.id = ?1 and o.position >= ?3 and o.position <= ?2";
			} else {
				jpqlUpdateNode = "update MenuNode as o set o.position = o.position - 1 where o.parentNode is null and o.position >= ?3 and o.position <= ?2";
			}
		}
		query1 = em.createQuery(jpqlUpdateNode);

		if (null != node.getParentNode() && !"".equals(node.getParentNode())) {
			query1.setParameter(1, node.getParentNode().getId());			
		}
		query1.setParameter(2, targetPosition);
		query1.setParameter(3, originalPosition);
		query1.executeUpdate();

		// 设置原Node节点位置
		jpqlSetNode = "update MenuNode as o set o.position = ?1 where o.id = ?2";
		Query query2 = em.createQuery(jpqlSetNode);
		query2.setParameter(1, targetPosition);
		query2.setParameter(2, nodeId);
		query2.executeUpdate();

	}

	
}
